package com.itheima.controller;

import com.itheima.pojo.Address;
import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于演示http参数的返回
 */
@RestController  //相当于下边两个注解
//    @Controller    是springMVC的组件，可以接收路径访问
//    @ResponseBody  每个请求的返回值经过了springMVC处理，返回的永远是字符串。
public class ReturnController {

    @RequestMapping("/jsonObjectReturn")
    public Address jsonObjectReturn(Address addr) {//返回类型虽然是Address,但实际上返回的是Address对象转成的Json字符串

        Address address = new Address();
        address.setProvince("江苏");
        address.setCity("滁州");

        return address;
        //{
        //    "province": "江苏",
        //    "city": "滁州"
        //}
    }

    @RequestMapping("/jsonArrayReturn")
    public List jsonArrayReturn(Address addr) {

        Address address = new Address();
        address.setProvince("江苏");
        address.setCity("滁州");

        Address address2 = new Address();
        address2.setProvince("安徽");
        address2.setCity("南京");

        List<Address> list = new ArrayList<>();
        list.add(address);
        list.add(address2);

        return list;
        //[
        //    {
        //        "province": "江苏",
        //        "city": "滁州"
        //    },
        //    {
        //        "province": "安徽",
        //        "city": "南京"
        //    }
        //]
    }

    /*由于返回的数据可能是字符串,对象,集合,这使得不同的函数返回值类型要不同,不便于书写,因此约定俗成使用一个自己定义Result类用于返回数据,Result中有
    Integer code;(1表示成功,0表示失败) String msg;(失败原因) Object data;(具体的数据字符串,对象,集合)
    返回的是Result类型的对象自动转成的Json字符串*/
    @RequestMapping("/jsonObjectReturnByResult")
    public Result jsonObjectReturn() {

        Address address = new Address();
        address.setProvince("江苏");
        address.setCity("滁州");

        Result result = new Result();
        result.setCode(1);
        result.setData(address);

        return result;
        //{
        //    "code": 1,
        //    "msg": null,
        //    "data": {
        //        "province": "江苏",
        //        "city": "滁州"
        //    }
        //}
    }

    //自己定义Result类用于返回数据,返回转成的Json字符串
    @RequestMapping("/jsonArrayReturnByResult")
    public Result jsonArrayReturn() {

        Address address = new Address();
        address.setProvince("江苏");
        address.setCity("滁州");

        Address address2 = new Address();
        address2.setProvince("安徽");
        address2.setCity("南京");

        List<Address> list = new ArrayList<>();
        list.add(address);
        list.add(address2);

        Result result = new Result();
        result.setCode(1);
        result.setMsg("example");
        result.setData(list);

        return result;
        //{
        //    "code": 1,
        //    "msg": "example",
        //    "data": [
        //        {
        //            "province": "江苏",
        //            "city": "滁州"
        //        },
        //        {
        //            "province": "安徽",
        //            "city": "南京"
        //        }
        //    ]
        //}
    }
}
